//
//  DoubleBallDrawing.m
//  SwiftTest
//
//  Created by mac on 14/10/31.
//  Copyright (c) 2014年 zhouy. All rights reserved.
//

#import "DoubleBallDrawing.h"
#import "DoubleColorBall.h"
#import "SelAction.h"
#import "DoubleTicket.h"

@implementation DoubleBallDrawing

//初始化彩球
-(void) initBll{
    NSMutableArray * arr= [[NSMutableArray alloc] init];
    
    for (int i=1,size=33; i<=size; i++ ) {
        DoubleColorBall * ball = [[DoubleColorBall alloc] init:@"Red" andNo:i];
        
        [arr addObject:ball];
    }
    
    for (int i=1,size=16; i<=size; i++ ) {
        DoubleColorBall * ball = [[DoubleColorBall alloc] init:@"Blue" andNo:i];
        
        [arr addObject:ball];
    }
    
    [self setBallArry:arr];
    
//    NSLog(@"初始化红球33个，初始化蓝球16个。完成。 ");
}

//随机一组彩球
-(DoubleTicket *) RandomDoubleTicket{
    [self initBll];
    
    DoubleTicket * ticket = [[DoubleTicket alloc] init];
    NSMutableArray * balArr = [[NSMutableArray alloc] init];
    
    NSMutableArray * ballAll = [self ballArry];
    
    //随机红球
    int redZoom = 32;
    for (int i=0,size=6; i<size; i++ ) {
        int x = (redZoom-i);
        int value = (arc4random() % x) + 1;
        
        [balArr addObject: ballAll[value]];
        [ballAll removeObjectAtIndex:value];
    }
    
    //随机篮球
    for (int i=0,size=1; i<size; i++ ) {
        int x = (redZoom-6-i);
        
        int value = (arc4random() % ([ballAll count] - x)) + x;
        
        [balArr addObject: ballAll[value]];
        [ballAll removeObjectAtIndex:value];
    }
    
    
    [ticket setReBallArry:balArr];
    
//    NSLog(@" %@",[ticket ticketStr]);
    
    return ticket;
}

//执行机选及验证规则
-(void) drawingStrat:(NSMutableArray *) actionArr{
    
    
    
    DoubleTicket * myTicket = [self RandomDoubleTicket];
    
    
    for (SelAction * itemAction in actionArr) {
        
        //调用所有验证机制
        //NSLog(@"\n开始验证：%@",[itemAction Name]);
        
        #pragma clang diagnostic push
        #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        bool reBool = [self performSelector:[itemAction MySel] withObject:myTicket];
        #pragma clang diagnostic pop
        
        //不通过其中一组验证机制 将递归调用
        //重新调用一次机选函数
        if (reBool) {
            //NSLog(@"\n验证机制不匹配：%@   重新生成机选号码。",[itemAction Name]);
            
            [self drawingStrat:actionArr ];
            return ;
        }
    }
    
    NSLog(@"\n随机号码为：\n%@ ",[myTicket ticketStr]);
    
//    va_list args;
//    va_start(args,selAction);
//    
//    SelAction * obj=selAction;
//    
//    do{
//        //移除编译警告
//        #pragma clang diagnostic push
//        #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
//        bool reBool = [self performSelector:[obj getMySel] withObject:doubleTicket];
//        
//        //如果验证方法返回True递归执行本方法
//        if (reBool) {
//            
//            [self drawingStrat:doubleTicket andSel:selAction ];
//        }
//        
//        #pragma clang diagnostic pop
//        
//        
//        obj=va_arg(args,SelAction *);//取得下一个指针
//        
//        
//        if([[obj Name] isEqual:nil]){
//            break;
//        }
//    }
//    while (true);
//    va_end(args);
//    
    
}

@end



